package cn.yunhe.services;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import cn.yunhe.dao.IHouseDao;
import cn.yunhe.model.Area;
import cn.yunhe.model.House;
import cn.yunhe.model.SouSuo;
import cn.yunhe.model.Street;
import cn.yunhe.model.Type;
import cn.yunhe.util.SqlSessionFactoryUtil;

/**
 * 
 * @author Administrator
 *
 */
public class HouseServices {
	SqlSession se = SqlSessionFactoryUtil.getSqlSession();
	IHouseDao houseDao = se.getMapper(IHouseDao.class);
	
	/**
	 * 添加住房
	 * @return
	 */
	public boolean addHouse(House house){
		boolean flag = houseDao.addHouse(house);
		se.commit();
		return flag;
	}
	/**
	 * 修改住房
	 * @return
	 */
	public boolean editHouse(House house){
		boolean flag = houseDao.editHouse(house);
		se.commit();
		System.out.println(flag);
		return flag;
	}
	/**
	 * 删除住房
	 * @return
	 */
	public boolean delhouse(int id){
		boolean flag = houseDao.delhouse(id);
		se.commit();
		return flag;
	}
	/**
	 * 查询所有住房
	 * @return
	 */
	public List<House> getAllHouse(int pageIndex,int pageCount){
		pageIndex = (pageIndex-1)*pageCount;
		List<House> list = houseDao.getAllHouse(pageIndex,pageCount);
		for (House house : list) {
			System.out.println(house.getFloorage());
		}
		return list;
	}
	/**
	 * 获得住房总数
	 * @return
	 */
	public int getHouseCount(){
		return houseDao.getHouseCount();
	}
	/**
	 *	多條件获取住房总量
	 * @param sousuo
	 * @return
	 */
	public int getHouseCountByTiaoJian(String title,String area,String street,String type,String floorage,String price,int currpage){
		float beginFloorage = 0;
		float endFloorage = 0;
		float beginPrice = 0;
		float endPrice = 0;
		int areaid = 0;
		int streetid = 0;
		int typeid = 0;
		
		title="%"+title+"%";
		if(!"".equals(floorage)&&floorage!=null){
			String floor[] = floorage.split("-");
			beginFloorage = Integer.parseInt(floor[0]);
			endFloorage = Integer.parseInt(floor[1]);
		}
		if(!"".equals(price)&&price!=null){
			String pri[] = price.split("-");
			beginPrice = Integer.parseInt(pri[0]);
			endPrice = Integer.parseInt(pri[1]);
		}
		if(!"".equals(area)&&area!=null){
			areaid=Integer.parseInt(area);
		}
		if(!"".equals(type)&&type!=null){
			typeid=Integer.parseInt(type);
		}
		if(!"".equals(street)&&street!=null){
			streetid=Integer.parseInt(street);
		}
		SouSuo souSuo = new SouSuo(title,areaid, streetid,typeid, beginFloorage, endFloorage,
				beginPrice ,endPrice, currpage);
		return houseDao.getHouseCountByTiaoJian(souSuo);
	}
	
	/**
	 * 根据条件获得住房
	 * @return
	 */
	public List<House> getHouseByTiaoJain(String title,String area,String street,String type,String floorage,String price,int currpage){
		float beginFloorage = 0;
		float endFloorage = 0;
		float beginPrice = 0;
		float endPrice = 0;
		int areaid = 0;
		int streetid = 0;
		int typeid = 0;
		currpage=(currpage-1)*SouSuo.pageCount;
		title="%"+title+"%";
		
		if(!"".equals(floorage)&&floorage!=null){
			String floor[] = floorage.split("-");
			beginFloorage = Integer.parseInt(floor[0]);
			endFloorage = Integer.parseInt(floor[1]);
		}
		if(!"".equals(price)&&price!=null){
			String pri[] = price.split("-");
			beginPrice = Integer.parseInt(pri[0]);
			endPrice = Integer.parseInt(pri[1]);
		}
		if(!"".equals(area)&&area!=null){
			areaid=Integer.parseInt(area);
		}
		if(!"".equals(type)&&type!=null){
			typeid=Integer.parseInt(type);
		}
		if(!"".equals(street)&&street!=null){
			streetid=Integer.parseInt(street);
		}
		
		System.out.println(beginFloorage + ":" + endFloorage + ":" + beginPrice + ":" + endPrice);
		SouSuo souSuo = new SouSuo(title,areaid, streetid,typeid, beginFloorage, endFloorage,
				beginPrice ,endPrice, currpage);
		return houseDao.getHouseByTiaoJain(souSuo);
	}
	/**
	 * 获得所有房型
	 * @return
	 */
	public List<Type> getAllType(){
		return houseDao.getAllType();
	}
	/**
	 * 获得所有区县
	 * @return
	 */
	public List<Area> getAllArea(){
		return houseDao.getAllArea();
	}
	/**
	 * 根据地区id获得所有街道
	 * @return
	 */
	public List<Street> getStreetByAreaId(int areaid){
		return houseDao.getStreetByAreaId(areaid);
	}
	/**
	 * 根据住房id获得住房详细信息
	 * @param houseId
	 * @return
	 */
	public House getHouseById(int houseId){
		return houseDao.getHouseById(houseId);
	}
}
